package com.orhon.smartcampus.modules.systemctl.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.base.dao.GradesDao;
import com.orhon.smartcampus.modules.base.dao.PeriodsDao;
import com.orhon.smartcampus.modules.base.dao.SubjectsDao;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.entity.Subjects;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPeriod;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSubject;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.PeriodMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.base.mm.SubjectMapper;
import com.orhon.smartcampus.modules.course.dao.CoursesDao;
import com.orhon.smartcampus.modules.course.entity.Courses;
import com.orhon.smartcampus.modules.course.gqlModel.GQLCourses;
import com.orhon.smartcampus.modules.course.mm.CoursesMapper;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDutiesDao;
import com.orhon.smartcampus.modules.systemctl.dao.SchoolSettingsDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDuties;
import com.orhon.smartcampus.modules.systemctl.entity.Schoolsettings;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDuty;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLSchoolSettings;
import com.orhon.smartcampus.modules.systemctl.mm.DutiesMapper;
import com.orhon.smartcampus.modules.systemctl.mm.SchoolSettingsMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SchoolsettingFetcher implements DataFetcher {


    @Autowired
    SchoolSettingsDao schoolSettingsDao;

    @Autowired
    TokenService tokenService;

    @Autowired
    GradesDao gradesDao;

    @Autowired
    PeriodsDao periodsDao;

    @Autowired
    SubjectsDao subjectsDao;

    @Autowired
    OrgDutiesDao orgDutiesDao;

    @Autowired
    CoursesDao coursesDao;

    /**
     * 获取学校设置
     *
     * @param environment
     * @return
     * @throws Exception editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);

        String hql = "from Schoolsettings where deleted_at = null and school_id = " + schoolId;
        Schoolsettings schoolsettings = schoolSettingsDao.findId(hql, null);

        if(schoolsettings!=null) {
            GQLSchool gqlSchools = null;
            if (environment.getSelectionSet().contains("school")){
                Schools school = schoolsettings.getBaseSchoolsBySchoolId();
                gqlSchools = SchoolMapper.MAPPER.toSchool(school);
            }

            List<GQLGrades> gqlGrades = new ArrayList<GQLGrades>();
            if (environment.getSelectionSet().contains("grades")){
                String gradesString = schoolsettings.getGrades().replace("[","").replace("]","").replace(" ", "");
                if(gradesString.length() > 0){
                    String[] gradesArray = gradesString.split(",");
                    //将String数组转成Integer数组
                    Integer[] integerArray = (Integer[]) ConvertUtils.convert(gradesArray, Integer.class);
                    for(Integer integer:integerArray){
                        String gradesHql = "from Grades where deleted_at = null and id = " + integer;
                        Grades grades = gradesDao.findId(gradesHql, null);
                        GQLGrades gqlGrade = GradeMapper.MAPPER.toGrade(grades);
                        gqlGrades.add(gqlGrade);
                    }
                }
            }

            List<GQLPeriod> gqlPeriods = new ArrayList<>();
            if (environment.getSelectionSet().contains("periods")){
                String periodsString = schoolsettings.getPeriods().replace("[","").replace("]","").replace(" ", "");
                if(periodsString.length() > 0){
                    String[] periodsArray = periodsString.split(",");
                    //将String数组转成Integer数组
                    Integer[] integerArray = (Integer[]) ConvertUtils.convert(periodsArray, Integer.class);
                    for(Integer integer:integerArray){
                        String subjectsHql = "from Periods where deleted_at = null and id = " + integer;
                        Periods periods = periodsDao.findId(subjectsHql, null);
                        GQLPeriod gqlPeriod = PeriodMapper.MAPPER.toPeriods(periods);
                        gqlPeriods.add(gqlPeriod);
                    }
                }
            }

            List<GQLSubject> gqlSubjects = new ArrayList<GQLSubject>();
            if (environment.getSelectionSet().contains("subjects")){
                String subjectsString = schoolsettings.getSubjects().replace("[","").replace("]","").replace(" ", "");
                if(subjectsString.length() > 0){
                    String[] subjectsArray = subjectsString.split(",");
                    //将String数组转成Integer数组
                    Integer[] integerArray = (Integer[]) ConvertUtils.convert(subjectsArray, Integer.class);
                    for(Integer integer:integerArray){
                        String subjectsHql = "from Subjects where deleted_at = null and id = " + integer;
                        Subjects subjects = subjectsDao.findId(subjectsHql, null);
                        GQLSubject gqlSubject = SubjectMapper.MAPPER.toSubject(subjects);
                        gqlSubjects.add(gqlSubject);
                    }
                }
            }
            List<GQLCourses> gqlCoursess = new ArrayList<>();
            if (environment.getSelectionSet().contains("courses")){
                String coursesString = schoolsettings.getCourses().replace("[","").replace("]","").replace(" ", "");
                if(coursesString.length() > 0){
                    String[] coursesArray = coursesString.split(",");
                    //将String数组转成Integer数组
                    Integer[] integerArray = (Integer[]) ConvertUtils.convert(coursesArray, Integer.class);
                    for(Integer integer:integerArray){
                        String subjectsHql = "from Courses where deleted_at = null and id = " + integer;
                        Courses courses = coursesDao.findId(subjectsHql, null);
                        GQLCourses gqlCourses = CoursesMapper.MAPPER.toCourses(courses);
                        gqlCoursess.add(gqlCourses);
                    }
                }
            }
            List<GQLDuty> gqlDutys = new ArrayList<GQLDuty>();
            if (environment.getSelectionSet().contains("duties")){
                String orgDutiesString = schoolsettings.getDuties().replace("[","").replace("]","").replace(" ", "");
                if(orgDutiesString.length() > 0){
                    String[] orgDutiesArray = orgDutiesString.split(",");
                    //将String数组转成Integer数组
                    Integer[] integerArray = (Integer[]) ConvertUtils.convert(orgDutiesArray, Integer.class);
                    for(Integer integer:integerArray){
                        String subjectsHql = "from OrgDuties where deleted_at = null and id = " + integer;
                        OrgDuties orgDuties = orgDutiesDao.findId(subjectsHql, null);
                        GQLDuty gqlDuty = DutiesMapper.MAPPER.toOrgDuties(orgDuties);
                        gqlDutys.add(gqlDuty);
                    }
                }
            }
            GQLSchoolSettings gqlGQLSchoolSettings = SchoolSettingsMapper.MAPPER.toSchoolsettings(schoolsettings);
            gqlGQLSchoolSettings.setDuties(gqlDutys);
            gqlGQLSchoolSettings.setGrades(gqlGrades);
            gqlGQLSchoolSettings.setPeriods(gqlPeriods);
            gqlGQLSchoolSettings.setSubjects(gqlSubjects);
            gqlGQLSchoolSettings.setSchool(gqlSchools);
            gqlGQLSchoolSettings.setCourses(gqlCoursess);
            return gqlGQLSchoolSettings;
        }
        return null;
    }
}
